package fr.upmc.tps.mdoc.servlets;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Login
 */
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Login() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.getRequestDispatcher("pages/accueil.jsp").forward(request,
				response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String user = request.getParameter("user");
		String pass = request.getParameter("pass");

		// Début: création d'utilisateurs
//		Session session;
//		Transaction tx;
//		session = HibernateUtil.getSessionFactory().openSession();
		// Utilisation de Spring pour instancier des utilisateurs
		/*
		 * ServletContext servletContext = getServletContext();
		 * ApplicationContext context = WebApplicationContextUtils
		 * .getWebApplicationContext(servletContext);
		 */

		// boolean b = false;
		// tx = session.beginTransaction();
		//
		// if (!b) {
		// User u;// = (User) context.getBean("user1");
		// u = new User();
		// u.setPasswd("aliboina");
		// u.setLogin("ali");
		// session.save(u);
		// tx.commit();
		//
		// tx = session.beginTransaction();
		// u = new User(); // (User) context.getBean("user2");// new User();
		// u.setLogin("alex");
		// u.setPasswd("alexhebert");
		// session.save(u);
		// tx.commit();
		// b = true;
		// }
		// DAOUser daou = new DAOUser();
		// User usd = daou.getUser(user, pass);
		//
		// // Debut: Création et ajout des groupes prédefinis
		// // session = HibernateUtil.getSessionFactory().openSession();
		// tx = session.beginTransaction();
		// ContactGroup g = new ContactGroup();
		// g.setGroupName("Amis");
		// session.save(g);
		//
		// g = new ContactGroup();
		// g.setGroupName("Collègues");
		// session.save(g);
		// g = new ContactGroup();
		// g.setGroupName("Famille");
		// session.save(g);
		// tx.commit();
		//
		// // fermeture de la session
		// session.close();
		// Fin

		// if (user.length() == 0 && pass.length() == 0) {
//		if (usd == null) {
//			request.getRequestDispatcher("pages/erreur.jsp").forward(request,
//					response);
//		} else {
//
//			// Compte utilisateur
//			request.getSession().setAttribute("existuser", true);
//			request.getSession().setAttribute("user", new User(user, pass));
//
//			request.getRequestDispatcher(
//					(usd == null) ? "./" : "pages/accueil.jsp").forward(
//					request, response);
//		}
	}

}
